package com.newbee.activity.regist;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import com.hhu.newbee.R;
import com.newbee.fragement.reg.RegEmailFragement;
import com.newbee.fragement.reg.RegInfoFragement;
import com.newbee.fragement.reg.RegSuccessFragement;
import com.newbee.viewpager.RegViewPager;
import com.newbee.viewpager.trans.ZoomOutPageTransformer;

public class RegisterActivity extends FragmentActivity
{

	private RegViewPager mViewPager;
	private List<Fragment> mFragments;
	private FragmentPagerAdapter mFragmentPagerAdapter;

	private String email;
	private String nickName;
	private String password;
	private Fragment infoFragement;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_reg);
		initViewPager();

	}

	private void initViewPager()
	{
		this.mViewPager = (RegViewPager) this.findViewById(R.id.viewpager_reg);
		this.mFragments = new ArrayList<Fragment>();
		this.mFragments.add(new RegEmailFragement());
		infoFragement = new RegInfoFragement();
		this.mFragments.add(infoFragement);
		this.mFragments.add(new RegSuccessFragement());
		changeViewPageScroller();
		mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
		mFragmentPagerAdapter = new FragmentPagerAdapter(
				getSupportFragmentManager())
		{

			@Override
			public int getCount()
			{
				// TODO Auto-generated method stub
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int posi)
			{
				// TODO Auto-generated method stub
				return mFragments.get(posi);
			}
		};
		mViewPager.setAdapter(mFragmentPagerAdapter);

	}

	public void goToNextPager(int nextIndex)
	{
		mViewPager.setCurrentItem(nextIndex);
	}

	private void changeViewPageScroller()
	{
		try
		{
			Field mField = ViewPager.class.getDeclaredField("mScroller");
			mField.setAccessible(true);
			FixedSpeedScroller scroller;
			scroller = new FixedSpeedScroller(this,
					new AccelerateDecelerateInterpolator());
			mField.set(mViewPager, scroller);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getNickName()
	{
		return nickName;
	}

	public void setNickName(String nickName)
	{
		this.nickName = nickName;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2)
	{

		super.onActivityResult(arg0, arg1, arg2);
	}

}

class FixedSpeedScroller extends Scroller
{
	private int mDuration = 800;

	public FixedSpeedScroller(Context context)
	{
		super(context);
	}

	public FixedSpeedScroller(Context context, Interpolator interpolator)
	{
		super(context, interpolator);
	}

	@Override
	public void startScroll(int startX, int startY, int dx, int dy, int duration)
	{
		super.startScroll(startX, startY, dx, dy, mDuration);
	}

	@Override
	public void startScroll(int startX, int startY, int dx, int dy)
	{
		super.startScroll(startX, startY, dx, dy, mDuration);
	}

	public void setmDuration(int time)
	{
		mDuration = time;
	}

	public int getmDuration()
	{
		return mDuration;
	}

};